//
//  LFRetweetView.swift
//  SDAutoWeibo
//
//  Created by 刘健 on 16/3/27.
//  Copyright © 2016年 Clemmie. All rights reserved.
//

import UIKit

import SnapKit

class LFRetweetView: UIView {
    //MARK: -- 属性
    var  retweetedViewBottomConstraint: Constraint?
    
    var retweetedStatus : LFStatus? {
        didSet {
            
            //判断 昵称
            guard let name = retweetedStatus?.user?.name else {
                
                print("没有昵称")
                return
            }
            
            guard let text = retweetedStatus?.text else {
                
                print("没有正文")
                return
            }
            //设置转发昵称和文本
            
            contentLabel.text = "@\(name): \(text)"
            
            //设置转发配图
            //有值  约束 重新约束转发View的底部
            retweetedViewBottomConstraint?.uninstall()
            if let thumbnailPicURLs = retweetedStatus?.thumbnailPicURLs {
                //有配图 就赋值给LFPictureView().picURLs
                picImageView.picURLs = thumbnailPicURLs
                snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetedViewBottomConstraint = make.bottom.equalTo(picImageView).constraint
                })
                //没有值 则转发View的底部依赖contentLabel
            }else {
                
                snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetedViewBottomConstraint = make.bottom.equalTo(contentLabel).constraint
                })
            }
            
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        
        addSubview(bgkButton)
        addSubview(contentLabel)
        addSubview(picImageView)
        
        bgkButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(LFOffSetDefault)
            make.trailing.equalTo(self).offset(-LFOffSetDefault)
        }
        
        picImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(LFOffSetDefault)
            make.leading.equalTo(self).offset(LFOffSetDefault)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.retweetedViewBottomConstraint = make.bottom.equalTo(contentLabel).constraint
        }
    }
    
    
    //背景按钮
    private lazy var bgkButton: UIButton =  {
        let btn = UIButton()
        
        btn .setBackgroundImage(UIImage(named:"retweet_background"), forState: UIControlState.Normal)
        btn .setBackgroundImage(UIImage(named:"retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return btn
    }()
    
    //内容label
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        
        //        label.text = "12441241241241225212"
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.textColor = UIColor.grayColor()
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * LFOffSetDefault
        
        return label
    }()
    
    //转发配图
    private lazy var picImageView: LFPictureView = LFPictureView()
    
}
